import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dali_ng_pera/page/certification/model/dnp_identity_model.dart';
import 'package:dali_ng_pera/page/certification/model/dnp_identity_upload_model.dart';
import 'package:dali_ng_pera/page/certification/utils/dnp_image_compression_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as dio;

import '../model/dnp_auton_model.dart';

class DnpIdentityPhotoController extends GetxController {
  final frontImageBytes = Rxn<int>();
  final frontIsCameraOpen = false.obs;

  final faceImageBytes = Rxn<int>();
  final faceIsCameraOpen = false.obs;

  String curCircumstances = "";
  final photoModel = Rxn<DnpIdentityModel>();
  final frontUploadModel = DnpIdentityUploadModel().obs;
  final faceUploadModel = DnpIdentityUploadModel().obs;

  void onRequestData(String already, {VoidCallback? callback}) {
    DnpPluginManager().showLoading();
    DnpNetManager()
        .post("/haseye/eis", data: dio.FormData.fromMap({"already": already}))
        .then((value) {
      DnpPluginManager().hideLoading();
      if (value.success) {
        try {
          photoModel.value = DnpIdentityModel.fromJson(value.close);
          callback?.call();
        } catch (e) {
          DnpPluginManager().showToast(e.toString());
        }
      } else {
        DnpPluginManager().showToast(value.kurios ?? "");
      }
    });
  }

  void onUploadImage(
      List<int>? bytes, bool isCameraOpen, int heard, String circumstances,
      {VoidCallback? callback}) async {
    ///"also":	int	否	图片来源:1拍照 2:相册选择
    /// "heard":	int	否	图片类型: 10:人像,11身份证正面
    /// "turnedIn"	File	否	图片文件
    /// "circumstances":	string	否	type=11时，证件类型： UMID/SSS/TIN/PASSPORT 等
    if (bytes == null) {
      return;
    }
    DnpPluginManager().showLoading();
    bytes = await DnpDecompressionUtils.compressImage(bytes);
    DnpNetManager()
        .uploadImage("/haseye/hemeis", imageBytes: bytes, queryParameters: {
      "also": isCameraOpen ? 1 : 2,
      "heard": heard,
      "circumstances": circumstances,
    }).then((value) {
      DnpPluginManager().hideLoading();
      if (value.success && value.iesous == "0") {
        try {
          if (heard == 10) {
            faceUploadModel.value =
                DnpIdentityUploadModel.fromJson(value.close);
          } else {
            frontUploadModel.value =
                DnpIdentityUploadModel.fromJson(value.close);
          }
          callback?.call();
        } catch (e) {
          DnpPluginManager().showToast(e.toString());
        }
      } else {
        DnpPluginManager().showToast(value.kurios ?? "");
      }
    });
  }

  void onSubmit(DnpIdentityUploadModel model,String heard,{VoidCallback? callback}) {
    DnpPluginManager().showLoading();
    DnpNetManager().post("/haseye/kai",
        data: dio.FormData.fromMap({
          "st":
              "${model.business?.padLeft(2, "0")}/${model.worshipper?.padLeft(2, "0")}/${model.ceremonial}}",
          "suburb": model.random,
          "beersheba": model.beersheba,
          "heard": heard,
          "circumstances": curCircumstances
        })).then((value){
          DnpPluginManager().hideLoading();
          if(value.success && value.iesous == "0"){
            callback?.call();
          }else{
            DnpPluginManager().showToast(value.kurios ?? "");
          }
    });
  }

  void onRequestNext(String already,ValueChanged<String>  callback) {
    DnpPluginManager().showLoading();
    DnpNetManager()
        .post("/haseye/auton", data: dio.FormData.fromMap({"already": already}))
        .then((value) {
      DnpPluginManager().hideLoading();
      if (value.success) {
        try {
          DnpAutonModel model = DnpAutonModel.fromJson(value.close);
          callback(model.boldly?.developing ?? "");
        } catch (e) {
          DnpPluginManager().showToast(e.toString());
        }
      } else {
        DnpPluginManager().showToast(value.kurios ?? "");
      }
    });
  }
}
